package com.cjl.interview150.doublePointer;

import org.junit.jupiter.api.Test;

public class DoublePointerPractice {

    /**
     * 125. 验证回文串
     *
     * @param s
     * @return
     */
    public boolean isPalindrome(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
//            if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
            if (Character.isLetterOrDigit(c)) {
                sb.append(c);
            }
        }
        int left = 0;
        int right = sb.length() - 1;
        // sb = new StringBuilder(sb.toString().toLowerCase());
        while (left <= right) {
            if (Character.toLowerCase(sb.charAt(left)) != Character.toLowerCase(sb.charAt(right))) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    /**
     * 167. 两数之和 II - 输入有序数组
     *
     * @param numbers
     * @param target
     * @return
     */
    public int[] twoSum(int[] numbers, int target) {
        int left = 0;
        int right = numbers.length - 1;
        while (left < right) {
            int sum = numbers[left] + numbers[right];
            if (sum > target) {
                right--;
            } else if (sum < target) {
                left++;
            } else {
                break;
            }
        }
        return new int[]{left + 1, right + 1};
    }

    /**
     * 11. 盛最多水的容器
     *
     * @param height
     * @return
     */
    public int maxArea(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int area = 0;
        while (left < right) {
            // 每次都移动短的那一端则一定可以获得最大的面积
            area = height[left] > height[right]
                    ? Math.max(area, (right - left) * height[right--])
                    : Math.max(area, (right - left) * height[left++]);
        }
        return area;
    }

}
